<?php
require Yii::app()->basePath.'/vendor/autoload.php';
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
define("AUTHORIZENET_LOG_FILE","phplog");
class AuthorizeNetAIMType{
    public $login_id;
    public $transactionKey;
    public $sandBox;
    public $fields=[];
    
    public function __construct($loginId,$transactionKey){
        $this->login_id=$loginId;
        $this->transactionKey=$transactionKey;
    }
    
    public function setSandbox($sandBox=true){
        $this->sandBox=$sandBox;
    }
    
    public function setFields($fields){
        if (!empty($fields)){
            $this->fields=$fields;
        }
    }
    
    public function authorizeAndCapture(){
        $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
        $merchantAuthentication->setName($this->login_id);
        $merchantAuthentication->setTransactionKey($this->transactionKey);
        $refId = 'ref' . time();
        
        $creditCard = new AnetAPI\CreditCardType();
        $creditCard->setCardNumber($this->fields['card_num']);
        $creditCard->setExpirationDate($this->fields['exp_date']);
        $paymentOne = new AnetAPI\PaymentType();
        $paymentOne->setCreditCard($creditCard);
        
        /**
         *  设置用户信息，物流信息，交易信息等
         */
        $customer=new AnetAPI\CustomerDataType();
        $customer->setId($this->fields['customer_id']);
        $customer->setEmail($this->fields['email']);
        
        $shipto = new AnetAPI\NameAndAddressType();
        $shipto->setAddress($this->fields['address']);
        $shipto->setFirstName($this->fields['first_name']);
        $shipto->setLastName($this->fields['last_name']);
        $shipto->setCity($this->fields['city']);
        $shipto->setState($this->fields['state']);
        $shipto->setCountry($this->fields['country']);
        $shipto->setZip($this->fields['zip']);
        
        $billto = new AnetAPI\CustomerAddressType();
        $billto->setFirstName($this->fields['first_name']);
        $billto->setLastName($this->fields['last_name']);
        $billto->setAddress($this->fields['address']);
        $billto->setCity($this->fields['city']);
        $billto->setState($this->fields['state']);
        $billto->setZip($this->fields['zip']);
        $billto->setCountry($this->fields['country']);
        
        /**
         *  创建交易，发送请求，获取返回信息
         */
        $transactionRequestType = new AnetAPI\TransactionRequestType(); //交易请求类
        $transactionRequestType->setTransactionType("authCaptureTransaction");//交易类型这个是什么意思？
        $transactionRequestType->setAmount($this->fields['amount']);//设置交易金额
        $transactionRequestType->setPayment($paymentOne);//设置交易，把上面那个支付类型类传递过来
        $transactionRequestType->setCustomer($customer);
        $transactionRequestType->setBillTo($billto);
        $transactionRequestType->setShipTo($shipto);
        $request = new AnetAPI\CreateTransactionRequest();//创建一个创建交易请求类
        $request->setMerchantAuthentication($merchantAuthentication);//设置验证类为上面那个类
        $request->setRefId( $refId);//设置ref值
        $request->setTransactionRequest($transactionRequestType);//设置交易请求
        $controller = new AnetController\CreateTransactionController($request);//创建 创建交易控制器类
        if ($this->sandBox){
            $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
        }else{
            $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::PRODUCTION);
        }
        return $response;
        
        /**
         * 如果改回BrainTree的话，把上面代码注释掉，把下面代码注释去掉
         */
        /* if ($response != null)
        {
            $tresponse = $response->getTransactionResponse();
            if (($tresponse != null) && ($tresponse->getResponseCode()=="1"))
            {
                echo "Charge Credit Card AUTH CODE : " . $tresponse->getAuthCode() . "\n";
                echo "Charge Credit Card TRANS ID  : " . $tresponse->getTransId() . "\n";
            }
            else
            {
                echo "Charge Credit Card ERROR :  Invalid response\n";
            }
        }
        else
        {
            echo  "Charge Credit Card Null response returned";
        }     */
    }
}